package com.lmaos.basetools.queue3;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

import com.lmaos.basetools.proxy.BeanProxy;
import com.lmaos.basetools.proxy.MethodIntercept;
import com.lmaos.basetools.proxy.autoproxy.BeanAutoProxy;

public class AsyncQueue3Proxy implements AsyncQueue3 {
	BeanProxy bproxy = new BeanAutoProxy();
	Map<String, Object> execBean = new ConcurrentHashMap<String, Object>(64);
	Map<String, AsyncResultCallback> asyncResultCallbackBean = new ConcurrentHashMap<String, AsyncResultCallback>(64);
	private ThreadPoolExecutor threadPoolExecutor;
	private AsyncResultCallback asyncResultCallback;
	public AsyncQueue3Proxy() {
		
		MethodIntercept intercept = new AsyncIntercept(this);
		bproxy.setMethodInterceot(intercept);
	}
	/** 指定线程池
	 * 
	 * @param threadPoolExecutor
	 */
	public AsyncQueue3Proxy(ThreadPoolExecutor threadPoolExecutor) {
		this();
		this.threadPoolExecutor = threadPoolExecutor;
	}

	@Override
	public Object getBean(String className) {
		Object value = execBean.get(className);
		if(value == null){
			bproxy.addBean(className);
		}
		return bproxy.getBean(className);
	}

	@Override
	public <T> T getBean(Class<T> clazz) {
		Object value = getBean(clazz.getName());
		if(value == null){
			return null;
		}
		return (T)value;
	}

	@Override
	public ThreadPoolExecutor setThreadPoolExecutor(
			ThreadPoolExecutor threadPoolExecutor) {
		if(this.threadPoolExecutor ==null && threadPoolExecutor!=null )
			this.threadPoolExecutor = threadPoolExecutor;
		return threadPoolExecutor;
	}
	@Override
	public ThreadPoolExecutor getThreadPoolExecutor() {
		return this.threadPoolExecutor;
	}
	@Override
	public void close() {
		this.threadPoolExecutor.shutdown();
		this.threadPoolExecutor = null;
	}
	@Override
	public void setAsyncResultCallback(AsyncResultCallback asyncResultCallback) {
		this.asyncResultCallback = asyncResultCallback;
		
	}
	@Override
	public AsyncResultCallback getAsyncResultCallback() {
		return this.asyncResultCallback;
	}
	@Override
	public void addAsyncResultCallback(String className,
			AsyncResultCallback asyncResultCallback) {
		asyncResultCallbackBean.put(className, asyncResultCallback);		
	}
	@Override
	public void addAsyncResultCallback(Class clazz,
			AsyncResultCallback asyncResultCallback) {
		addAsyncResultCallback(clazz.getName(), asyncResultCallback);
	}
	@Override
	public AsyncResultCallback getAsyncResultCallback(String className) {
		AsyncResultCallback asyncResultCallback = asyncResultCallbackBean.get(className);
		if(asyncResultCallback == null){
			asyncResultCallback = this.asyncResultCallback;
		}
		return asyncResultCallback;
	}
	@Override
	public AsyncResultCallback getAsyncResultCallback(Class clazz) {
		return getAsyncResultCallback(clazz.getName());
	}

}
